<template>
	<u-popup v-model="show" mode="bottom" border-radius="10" :safe-area-inset-bottom="true">
		<view class="content_box">
			<view class="title">选择收货地址</view>
			<view class="address_list_box">
				<view class="address_item" v-for="(item, index) in list" :key="index" @click="choseAddress(item)">
					<view class="address_item_content">
						<view class="left">
							<view class="user_info">
								<u-tag
									v-if="item.isDefaultAddress === 1"
									text="默认"
									mode="dark"
									shape="circle"
									type="warning"
									size="mini"
									style="margin-right: 20rpx"
								/>
								<text class="user_name">{{ item.contactName }}</text>
								<text class="user_tel">{{ item.contractPhone }}</text>
							</view>
							<view class="address_text">{{ item.address }}</view>
						</view>
						<u-icon name="edit-pen" color="#888" size="32" v-show="false"></u-icon>
					</view>
					<u-line margin="24rpx" />
				</view>
				<u-button type="warning" v-if="list.length === 0" @click="addNewAddress">新增地址</u-button>
			</view>
		</view>
	</u-popup>
</template>

<script>
import { userAddressList } from '@/api/user'

export default {
	name: 'AddressSelection',
	data() {
		return {
			show: false,
			list: []
		}
	},
	created() {
		// this.getUserAddressList()
	},
	methods: {
		openPopup() {
			this.getUserAddressList()
			this.show = true
		},
		async getUserAddressList() {
			const { data } = await userAddressList()
			this.list = data
		},
		choseAddress(item) {
			this.$emit('getAddress', item)
			this.show = false
		},
		addNewAddress() {
			this.show = false
			uni.navigateTo({
				url: '/pages/my/addAddress'
			})
		},
		async getDefaultAddress() {
			const { data } = await userAddressList()
			const info = data.find((i) => i.isDefaultAddress === 1)
			console.log(info)
			if (info) {
				this.$emit('getDefaultAddress', info)
			} else {
				this.$emit('getDefaultAddress', {})
			}
		}
	}
}
</script>

<style lang="scss" scoped>
.content_box {
	padding: 26rpx 40rpx;

	.title {
		font-size: 36rpx;
		text-align: center;
	}

	.address_list_box {
		margin-top: 36rpx;

		.address_item {
			margin-bottom: 24rpx;

			.address_item_content {
				display: flex;
				align-items: center;
				justify-content: space-between;

				.left {
					max-width: 500rpx;

					.user_info {
						font-size: 32rpx;
						display: flex;
						align-items: center;

						.user_tel {
							margin-left: 20rpx;
						}
					}

					.address_text {
						margin-top: 8rpx;
						font-size: 26rpx;
					}
				}
			}
		}
	}
}
</style>
